package org.sky.framework.rest;

import java.util.Collection;

import com.github.pagehelper.Page;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

@ApiModel(description = "分页查询实体")
public class PageEntity<T> extends RestEntity<T> {

	@ApiModelProperty(value = "查询页数")
	protected Integer pageNum = 1;

	@ApiModelProperty(value = "每页数量")
	protected Integer pageSize = 10;

	@ApiModelProperty(value = "总数量")
	protected Long totalNum = 0L;

	public PageEntity() {
	}

	/**
	 * 包装{@link com.github.pagehelper.Page}对象
	 *
	 * @param list
	 */
	public PageEntity(T list) {
		this.entity = list;
		if (list instanceof Collection<?>) {
			Collection<?> array = (Collection<?>) list;
			if (list instanceof Page) {
				Page<?> page = (Page<?>) list;
				this.totalNum = page.getTotal();
				this.pageNum = page.getPageNum();
				this.pageSize = page.getPageSize();
			} else {
				this.totalNum = new Long(array.size());
				this.pageNum = 1;
				this.pageSize = array.size();
			}
		}
	}

	public static <T> PageEntity<T> of(T list) {
		return new PageEntity<T>(list);
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Long getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(Long totalNum) {
		this.totalNum = totalNum;
	}

}
